package com.soc.web.client;

import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ImageElement;
import com.soc.web.client.api.Element;
import com.soc.web.client.api.FancyBoxException;
import com.soc.web.client.api.FancyboxUtils;
import com.soc.web.client.api.markers.Loadable;

public class Media extends Element implements Loadable<AnchorElement> {
	// Options
	private boolean externallyLoaded;
	
	// the link part
	private String href;
	private String title;
	private String linkId;
		
	// DOM Elements
	private AnchorElement anchor;
	private ImageElement image;
	
	public Media() {
		anchor = Document.get().createAnchorElement();
		image = Document.get().createImageElement();	
		externallyLoaded = false; /* By default the class that extends this one will load itself. This is important for gallery when it will be loading is own fancybox context */
		setElement(anchor);
	}
	
	@Override
	public AnchorElement load() throws FancyBoxException {
		// Do some checks
		if(this.thumbnail == null && this.displayText == null) {
			throw new FancyBoxException("There was no thumbnail / display text to present.");
		} else if(this.thumbnail != null) {
			showImage();
		} else if(this.displayText != null && this.displayText.length() > 0){
			showText(); // Display some text instead of a 
		} else {
			throw new FancyBoxException("You've got to put something here. A thumbnail or a text to display.");
		}
		
		// Take all the values that have been set and load the components.
		this.linkId = FancyboxUtils.getUniqueId();
		
		anchor.setId(this.linkId);
		anchor.setHref(this.href);
		
		// If it has a title then add it.
		if(title != null && title.length() > 0) {
			anchor.setTitle(title);
		}
		
		// If it is externally loaded, then don't wrap the element in fancybox. Let the controlling element do that.
		if(!externallyLoaded) {
			fancybox(anchor);
		}
		return anchor;
	}
	
	private void showText() {
		anchor.setInnerHTML(this.displayText);
	}

	private void showImage() {
		// Set the source and the alternative text from the thumbnail.		
		image.setSrc(this.thumbnail.getUrl());
		image.setAlt(this.thumbnail.getAltText());
		anchor.appendChild(image);
	}
	
	@Override
	protected void onLoad() {
		try {
			load();
		} catch (FancyBoxException e) {
			e.printStackTrace();
		}		
		 // Initializes the fancybox context for the anchor point.
		super.onLoad();
	}
	
	public String getHref() {
		return href;
	}
	
	public void setHref(String href) {
		this.href = href;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	

	public AnchorElement getAnchor() {
		return anchor;
	}

	public ImageElement getImage() {
		return image;
	}

	public String getTitle() {
		return title;
	}
	
	public void setExternallyLoaded(boolean externallyLoaded) {
		this.externallyLoaded = externallyLoaded;
	}
	
	public boolean isExternallyLoaded() {
		return externallyLoaded;
	}
}
